package com.mesSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mesSystem.dto.Result;
import com.mesSystem.entity.QualityInspection;
import com.mesSystem.entity.WorkstationType;
import com.mesSystem.service.impl.QualityInspectionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@ResponseBody
@RequestMapping("QualityInspection")
public class QualityInspectionController {
    @Autowired
    QualityInspectionService qualityInspectionService;

    /**
     * 新增--质检定义
     * @param qualityInspection
     * @return
     */
    @PostMapping("increase")
    public Result increase(@RequestBody QualityInspection qualityInspection) {
        log.info(qualityInspection.toString());
        qualityInspectionService.save(qualityInspection);
        return Result.ok("创建成功");
    }

    @DeleteMapping("delete")
    public Result delete(Long qualityInspectionId) {
        qualityInspectionService.removeById(qualityInspectionId);
        return Result.ok("删除成功");
    }

    /**
     * 分页查询信息
     */
    @GetMapping("query")
    public Result query(@RequestParam(value = "page", defaultValue = "1") Integer page,
                        @RequestParam(value = "size", defaultValue = "10") Integer size) {
        // 创建分页器对象
        Page<QualityInspection> Page = new Page<>(page, size);

        // 调用分页查询方法，将分页器传递给service
        qualityInspectionService.page(Page, new QueryWrapper<QualityInspection>().orderByAsc("quality_inspection_definition_id"));

        // 从分页器对象中获取分页后的数据
        List<QualityInspection> list = Page.getRecords();

        log.info("被查询信息为：{}", list);

        // 返回分页结果
        return Result.ok(list);
    }

    /**
     * 更改
     */
    @PostMapping("update")
    public Result update(@RequestBody QualityInspection qualityInspection) {
        qualityInspectionService.updateById(qualityInspection);
        log.info("update:{}", qualityInspection);
        return Result.ok("更新成功");
    }

}
